// auto.waitFor("normal") 
// engines.stopAll()


auto();
events.observeKey();
events.on("key", function(keyCode, event){
    toastLog("按下音量减键按下---");
    sendMsgToFeishu()
});



function sendMsgToFeishu(){
    try {
        url = "https://open.feishu.cn/open-apis/bot/v2/hook/0c8678b8-4e2f-4a6e-886e-c3ca22a6d33c"
        r = http.postJson(url, {
            "msg_type":"text",
            "content":{
                "text": "测试定时任务 " + getCurrentDate()+' '+getCurrentTime()
            }
        });
        if(r.statusCode >= 200 && r.statusCode < 300){
            toastLog("测试定时任务---");
        }else{
            throw new Error("测试定时任务失败");
        }
    } catch (e) {
        toastLog("测试定时任务失败"+e);
        url = "https://open.feishu.cn/open-apis/bot/v2/hook/0c8678b8-4e2f-4a6e-886e-c3ca22a6d33c"
        r = http.postJson(url, {
            "msg_type": "text",
            "content": {
                "text": e
            }
        });
    }
}
// =========================================
//  功能函数
// =========================================

function dateDigitToString(num){
    return num < 10 ? '0' + num : num
}

function getCurrentTime(){
    var currentDate = new Date()
    var hours = dateDigitToString(currentDate.getHours())
    var minute = dateDigitToString(currentDate.getMinutes())
    var second = dateDigitToString(currentDate.getSeconds())
    var formattedTimeString = hours + ':' + minute + ':' + second
    return formattedTimeString
}

function getCurrentDate(){
    var weekday = new Array(7);
    weekday[0] = "Sunday"
    weekday[1] = "Monday"
    weekday[2] = "Tuesday"
    weekday[3] = "Wednesday"
    weekday[4] = "Thursday"
    weekday[5] = "Friday"
    weekday[6] = "Saturday"
    var currentDate = new Date()
    var year = dateDigitToString(currentDate.getFullYear())
    var month = dateDigitToString(currentDate.getMonth() + 1) // Date.getMonth()的返回值是0-11,所以要+1
    var date = dateDigitToString(currentDate.getDate())
    var week = currentDate.getDay()
    var formattedDateString = year + '-' + month + '-' + date + '-' + weekday[week]
    return formattedDateString
}